/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.service.impl;

import com.koron.purpose.mapper.SafetyInvestmentMapper;
import com.koron.purpose.service.api.SafetyInvestmentService;
import com.koron.purpose.utils.BusinessCode;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("deprecation")
public class SafetyInvestmentServiceImpl implements SafetyInvestmentService {
    private final Logger logger = LoggerFactory.getLogger(SafetyInvestmentServiceImpl.class);
    @Override
    public MessageBean statisticalQuery(String corpCode, Integer year, String manager, String serialNumber, String contributoryProject) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), void.class);
        try (SessionFactory factory = new SessionFactory()) {
            SafetyInvestmentMapper mapper = factory.getMapper(SafetyInvestmentMapper.class);
            // 安全投入统计 计划投入总额、实际投入总额查询
            List<Map> list = mapper.queryTotalSum(corpCode, year, manager, serialNumber, contributoryProject);
            if (Optional.of(list).isPresent()) {
                // 安全投入统计 实际投入明细查询
                List<Map> realDetails = mapper.queryRealDetails(corpCode, year, manager, serialNumber, contributoryProject);
                if (realDetails != null && realDetails.size() > 0) {
                    list = list.stream().map(map -> {
                        // 如果 实际投入总额 大于0
                        if (Double.valueOf(map.get("realityTotalSum").toString()) > 0.00) {
                            String contributoryPlanID = Optional.of(map.get("contributoryPlanID")).orElse("").toString();
                            String planDetailId = Optional.of(map.get("planDetailId")).orElse("").toString();
                            if (StringUtils.isNotEmpty(contributoryPlanID) && StringUtils.isNotEmpty(planDetailId)) {
                                realDetails.stream().forEach(realDetail -> {
                                    if (contributoryPlanID.equals(realDetail.get("contributoryPlanID").toString())
                                            && planDetailId.equals(realDetail.get("planDetailId").toString())) {
                                        map.put("realDetails", realDetail);
                                    }
                                });
                            }
                        }
                        return map;
                    }).collect(Collectors.toList());
                }
            }
            msg.setData(list);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription("安全投入统计查询失败!");
        }
        return msg;
    }

    @Override
    public List<Map> exportQuery(String corpCode, Integer year, String manager, String serialNumber, String contributoryProject) {
        List<Map> list = null;
        try (SessionFactory factory = new SessionFactory()) {
            SafetyInvestmentMapper mapper = factory.getMapper(SafetyInvestmentMapper.class);
            // 安全投入统计 计划投入总额、实际投入总额查询
            list = mapper.queryTotalSum(corpCode, year, manager, serialNumber, contributoryProject);
            if (Optional.of(list).isPresent()) {
                // 安全投入统计 实际投入明细查询
                List<Map> realDetails = mapper.queryRealDetails(corpCode, year, manager, serialNumber, contributoryProject);
                if (realDetails != null && realDetails.size() > 0) {
                    list = list.stream().map(map -> {
                        // 如果 实际投入总额 大于0
                        if (Double.valueOf(map.get("realityTotalSum").toString()) > 0.00) {
                            String contributoryPlanID = Optional.of(map.get("contributoryPlanID")).orElse("").toString();
                            String planDetailId = Optional.of(map.get("planDetailId")).orElse("").toString();
                            if (StringUtils.isNotEmpty(contributoryPlanID) && StringUtils.isNotEmpty(planDetailId)) {
                                realDetails.stream().forEach(realDetail -> {
                                    if (contributoryPlanID.equals(realDetail.get("contributoryPlanID").toString())
                                            && planDetailId.equals(realDetail.get("planDetailId").toString())) {
                                        map.put("realDetails", realDetail);
                                    }
                                });
                            }
                        }
                        return map;
                    }).collect(Collectors.toList());
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return list;
    }
}
